package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lxmes.business.domain.*;
import com.lxmes.business.mapper.*;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.sales.domain.NcSalesOrder;
import com.lxmes.sales.mapper.NcSalesOrderMapper;
import com.lxmes.system.mapper.SysSpMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.service.ICcChukuInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库单Service业务层处理
 *
 * @author 张志煜
 * @date 2022-11-27
 */
@Service
public class CcChukuInfoServiceImpl implements ICcChukuInfoService
{
    @Autowired
    private CcChukuInfoMapper ccChukuInfoMapper;
    @Autowired
    private CcStockInfoMapper stockInfoMapper;
    @Autowired
    private CcChukuSpgzMapper spgzMapper;
    @Autowired
    private CcChukuSprwMapper sprwMapper;
    @Autowired
    private SysSpMapper spMapper;

    @Autowired
    private NcSalesOrderMapper ncSalesOrderMapper;

    @Autowired
    private ZcOrderCostAccountingMapper zcOrderCostAccountingMapper;

    @Autowired
    private ZcOrderCostSummaryMapper zcOrderCostSummaryMapper;
    /**
     * 查询出库单
     *
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public CcChukuInfo selectCcChukuInfoById(Long id)
    {
        return ccChukuInfoMapper.selectCcChukuInfoById(id);
    }

    /**
     * 查询出库单列表
     *
     * @param ccChukuInfo 出库单
     * @return 出库单
     */
    @Override
    public List<CcChukuInfo> selectCcChukuInfoList(CcChukuInfo ccChukuInfo)
    {
        ccChukuInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        return ccChukuInfoMapper.selectCcChukuInfoList(ccChukuInfo);
    }

    @Override
    public List<CcChukuInfo> selectCcChukuInfoMainList(CcChukuInfo ccChukuInfo) {
        ccChukuInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        return ccChukuInfoMapper.selectCcChukuInfoMainList(ccChukuInfo);
    }

    @Override
    public List<CcChukuInfo> selectCcChukuInfoLzList(CcChukuInfo ccChukuInfo) {
        ccChukuInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        ccChukuInfo.setReceivingUnit(SecurityUtils.getDeptId().toString());
        return ccChukuInfoMapper.selectCcChukuInfoLzList(ccChukuInfo);
    }
    //首页大屏出库数据
    @Override
    public List<CcChukuInfo> selectCcChukuInfoDaBbList(CcChukuInfo ccChukuInfo) {
        return ccChukuInfoMapper.selectCcChukuInfoDaBbList(ccChukuInfo);
    }

    @Override
    public List<CcChukuInfo> selectCcChukuInfoJieSuanList(CcChukuInfo ccChukuInfo) {
        ccChukuInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        return ccChukuInfoMapper.selectCcChukuInfoJieSuanList(ccChukuInfo);
    }

    /**
     * 新增出库单
     *
     * @param ccChukuInfo 出库单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCcChukuInfo(CcChukuInfo ccChukuInfo)
    {
        String number= "CK-"+ NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
        List<CcChukuInfo> list = ccChukuInfo.getInfoList();
        if(list.size()>0){
            for ( CcChukuInfo info:list) {
                info.setDanhao(number);
                info.setFactoryName(SecurityUtils.getFactoryName());
                info.setFactoryCode(SecurityUtils.getFactoryCode());
                info.setChukuDate(ccChukuInfo.getChukuDate());
                info.setChukuType(ccChukuInfo.getChukuType());
                info.setOrderType(ccChukuInfo.getOrderType());
                info.setOrderDanhao(StringUtils.isNotEmpty(ccChukuInfo.getOrderDanhao())?ccChukuInfo.getOrderDanhao():info.getOrderDanhao());
                info.setContractCode(StringUtils.isNotEmpty(ccChukuInfo.getContractCode())?ccChukuInfo.getContractCode():info.getContractCode());
                info.setPlanId(StringUtils.isNotEmpty(ccChukuInfo.getPlanId())?ccChukuInfo.getContractCode():info.getPlanId());
                info.setClientName(ccChukuInfo.getClientName());
                info.setClientCode(ccChukuInfo.getClientCode());
                info.setPickingName(ccChukuInfo.getPickingName());
                info.setTxm(ccChukuInfo.getTxm());
                info.setPickingSegment(ccChukuInfo.getPickingSegment());
                info.setPickingTime(ccChukuInfo.getPickingTime());
                info.setZubie(ccChukuInfo.getZubie());
                info.setRemark(ccChukuInfo.getRemark());
                info.setCreateTime(DateUtils.getNowDate());
                info.setCreateBy(SecurityUtils.getUsername());
                info.setReceivingStockId(ccChukuInfo.getReceivingStockId());
                info.setReceivingUnit(ccChukuInfo.getReceivingUnit());
                //记录出库的唯一库存
                info.setStockinfoId(info.getId());
               /* CcStockInfo stockInfo = stockInfoMapper.selectCcStockInfoById(info.getId());
                if(stockInfo.getLockNum().add(info.getCkNum()).compareTo(stockInfo.getKcNum())==1){
                    throw new ServiceException(info.getItemCode()+":可用数量已超出库存数量！");
                }
                stockInfo.setLockNum(stockInfo.getLockNum().add(info.getCkNum()));
                stockInfo.setKyNum(stockInfo.getKyNum().subtract(info.getCkNum()));
                stockInfoMapper.updateCcStockInfo(stockInfo);*/
               //不执行直接出库
              /*  info.setCkState(1L);
                this.handStockInfo(info);*/
                info.setCkState(0L);
                this.handStockInfoZx(info);
                info.setId(null);
                ccChukuInfoMapper.insertCcChukuInfo(info);
            }
        }else {
            throw new ServiceException("物料不存在！");
        }
        return 1;
    }

    @Override
    public int insertCopyCcChukuInfo(CcChukuInfo ccChukuInfo) {
        String number= "CK-"+ NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
        List<CcChukuInfo> list = ccChukuInfo.getInfoList();
        if(list.size()>0){
            for ( CcChukuInfo info:list) {
                info.setDanhao(number);
                info.setFactoryName(SecurityUtils.getFactoryName());
                info.setFactoryCode(SecurityUtils.getFactoryCode());
                info.setChukuDate(ccChukuInfo.getChukuDate());
                info.setChukuType(ccChukuInfo.getChukuType());
                info.setOrderType(ccChukuInfo.getOrderType());
                info.setOrderDanhao(StringUtils.isNotEmpty(ccChukuInfo.getOrderDanhao())?ccChukuInfo.getOrderDanhao():info.getOrderDanhao());
                info.setContractCode(StringUtils.isNotEmpty(ccChukuInfo.getContractCode())?ccChukuInfo.getContractCode():info.getContractCode());
                info.setPlanId(StringUtils.isNotEmpty(ccChukuInfo.getPlanId())?ccChukuInfo.getContractCode():info.getPlanId());
                info.setClientName(ccChukuInfo.getClientName());
                info.setClientCode(ccChukuInfo.getClientCode());
                info.setPickingName(ccChukuInfo.getPickingName());
                info.setTxm(ccChukuInfo.getTxm());
                info.setPickingSegment(ccChukuInfo.getPickingSegment());
                info.setPickingTime(ccChukuInfo.getPickingTime());
                info.setZubie(ccChukuInfo.getZubie());
                info.setRemark(ccChukuInfo.getRemark());
                info.setCreateTime(DateUtils.getNowDate());
                info.setCreateBy(SecurityUtils.getUsername());
                info.setReceivingStockId(ccChukuInfo.getReceivingStockId());
                info.setReceivingUnit(ccChukuInfo.getReceivingUnit());
                //记录出库的唯一库存
                info.setStockinfoId(info.getId());
                info.setCkState(0L);
                this.autoChukuZx(info);
            }
        }else {
            throw new ServiceException("物料不存在！");
        }
        return 1;
    }

    @Override
    @Transactional
    public int insertSmCcChukuInfo(CcChukuInfo ccChukuInfo) {
        if(StringUtils.isBlank(ccChukuInfo.getTxm())){
            throw new RuntimeException("请填写设备编码！！！");
        }
        if(StringUtils.isBlank(ccChukuInfo.getClientName())){
            throw new RuntimeException("请填写客户名称！！！");
        }
        if(StringUtils.isBlank(ccChukuInfo.getZubie())){
            throw new RuntimeException("请填写出库原因！！！");
        }
        String number= "CK-"+ NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
        ccChukuInfo.setDanhao(number);
        ccChukuInfo.setJhckNum(ccChukuInfo.getCkNum());
        //出库日期
        ccChukuInfo.setChukuDate(DateUtils.getNowDate());
        CcStockInfo queryStockInfo=new CcStockInfo();
        //queryStockInfo.setLot(ccChukuInfo.getLot());
        queryStockInfo.setItemId(ccChukuInfo.getItemId());
        queryStockInfo.setKuweiCode(ccChukuInfo.getKuweiCode());
        queryStockInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        //先进先出批次正序
        List<CcStockInfo> stockInfoList = stockInfoMapper.selectCcStockInfoList2(queryStockInfo);
        if(stockInfoList.size()>0){
            //待出库数量
            BigDecimal dckNum=ccChukuInfo.getCkNum();
            for (CcStockInfo stockInfo:stockInfoList){
                //1.先计算可用数量
                BigDecimal  kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                //2.如果库存够直接扣除
                if(kyNum.compareTo(dckNum)>-1){
                    stockInfo.setKcNum(stockInfo.getKcNum().subtract(dckNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(dckNum));
                    stockInfo.setClientName(ccChukuInfo.getClientName());
                    stockInfo.setTxm(ccChukuInfo.getTxm());
                    //待执行接口
             /*       stockInfo.setLockNum(stockInfo.getLockNum().add(dckNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(dckNum));*/
                    ccChukuInfo.setCkNum(dckNum);

                    this.smCkSave(stockInfo,ccChukuInfo);
                    dckNum=new BigDecimal("0");
                    break; // 退出循环
                }else{
                    //否则不够库存继续循环
                    stockInfo.setKcNum(stockInfo.getKcNum().subtract(kyNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(kyNum));
                    stockInfo.setClientName(ccChukuInfo.getClientName());
                    stockInfo.setTxm(ccChukuInfo.getTxm());
                    //待执行接口
                   /* stockInfo.setLockNum(stockInfo.getLockNum().add(kyNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(kyNum));*/
                    ccChukuInfo.setCkNum(kyNum);
                    //不执行，直接出库
                    this.smCkSave(stockInfo,ccChukuInfo);
                    dckNum= dckNum.subtract(kyNum);
                }
            }
            if(dckNum.compareTo(new BigDecimal("0"))>0){
                throw new RuntimeException("出库失败库存不足！！！");
            }
        }else {
            throw new RuntimeException("无库存数据！！！");
        }
        return 1;
    }
    //自行出库不执行
    public void autoChuku(CcChukuInfo ccChukuInfo){
        String number= "CK-"+ NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
        ccChukuInfo.setDanhao(number);
        ccChukuInfo.setJhckNum(ccChukuInfo.getCkNum());
        CcStockInfo queryStockInfo=new CcStockInfo();
        //queryStockInfo.setLot(ccChukuInfo.getLot());
        queryStockInfo.setItemId(ccChukuInfo.getItemId());
        queryStockInfo.setKuweiCode(ccChukuInfo.getKuweiCode());
        queryStockInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        //先进先出批次正序
        List<CcStockInfo> stockInfoList = stockInfoMapper.selectCcStockInfoList2(queryStockInfo);
        if(stockInfoList.size()>0){
            //待出库数量
            BigDecimal dckNum=ccChukuInfo.getCkNum();
            for (CcStockInfo stockInfo:stockInfoList){
                //1.先计算可用数量
                BigDecimal  kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                //2.如果库存够直接扣除
                if(kyNum.compareTo(dckNum)>-1){
                    stockInfo.setKcNum(stockInfo.getKcNum().subtract(dckNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(dckNum));
                    ccChukuInfo.setCkNum(dckNum);
                    this.smCkSave(stockInfo,ccChukuInfo);
                    dckNum=new BigDecimal("0");
                    break; // 退出循环
                }else{
                    //否则不够库存继续循环
                    stockInfo.setKcNum(stockInfo.getKcNum().subtract(kyNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(kyNum));
                    ccChukuInfo.setCkNum(kyNum);
                    //不执行，直接出库
                    this.smCkSave(stockInfo,ccChukuInfo);
                    dckNum= dckNum.subtract(kyNum);
                }
            }
            if(dckNum.compareTo(new BigDecimal("0"))>0){
                throw new RuntimeException("出库失败库存不足！！！");
            }
        }else {
            throw new RuntimeException("无库存数据！！！");
        }

    }
    //自动出库需执行
    public void autoChukuZx(CcChukuInfo ccChukuInfo){
        CcStockInfo queryStockInfo=new CcStockInfo();
        //queryStockInfo.setLot(ccChukuInfo.getLot());
        queryStockInfo.setItemId(ccChukuInfo.getItemId());
        queryStockInfo.setKuweiCode(ccChukuInfo.getKuweiCode());
        queryStockInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        //先进先出批次正序
        List<CcStockInfo> stockInfoList = stockInfoMapper.selectCcStockInfoList2(queryStockInfo);
        if(stockInfoList.size()>0){
            //待出库数量
            BigDecimal dckNum=ccChukuInfo.getCkNum();
            for (CcStockInfo stockInfo:stockInfoList){
                //1.先计算可用数量
                BigDecimal  kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                //2.如果库存够直接扣除
                if(kyNum.compareTo(dckNum)>-1){
                    //待执行接口
                    stockInfo.setLockNum(stockInfo.getLockNum().add(dckNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(dckNum));
                    ccChukuInfo.setCkNum(dckNum);
                    this.smCkSaveZx(stockInfo,ccChukuInfo);
                    dckNum=new BigDecimal("0");
                    break; // 退出循环
                }else{
                    //否则不够库存继续循环
                    //待执行接口
                    stockInfo.setLockNum(stockInfo.getLockNum().add(kyNum));
                    stockInfo.setKyNum(stockInfo.getKyNum().subtract(kyNum));
                    ccChukuInfo.setCkNum(kyNum);
                    //待执行出库
                    this.smCkSaveZx(stockInfo,ccChukuInfo);
                    dckNum= dckNum.subtract(kyNum);
                }
            }
            if(dckNum.compareTo(new BigDecimal("0"))>0){
                throw new RuntimeException("出库失败库存不足！！！");
            }
        }else {
            throw new RuntimeException(ccChukuInfo.getItemId()+"出库失败库存不足！！！");
        }
    }
    //手动出库不执行
    public void handStockInfo(CcChukuInfo info){
        //查询库存的物料
        CcStockInfo stockInfo = stockInfoMapper.selectCcStockInfoById(info.getId());
        //减掉库存数量
        if(stockInfo.getKcNum().compareTo(info.getCkNum())==1 || stockInfo.getKcNum().compareTo(info.getCkNum())==0){
            stockInfo.setUpdateBy(SecurityUtils.getUsername());
            stockInfo.setUpdateTime(new Date());
            stockInfo.setKcNum(stockInfo.getKcNum().subtract(info.getCkNum()));
            //stockInfo.setLockNum(stockInfo.getLockNum().subtract(info.getCkNum()));
            stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
            stockInfo.setChukuId(info.getDanhao());
            stockInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(stockInfo.getKcNum()));
            stockInfoMapper.updateCcStockInfo(stockInfo);
        }else {
            throw new ServiceException("库存数量及出库数量错误！");
        }
    }
    //手动出库待执行
    public void handStockInfoZx(CcChukuInfo info){
        CcStockInfo stockInfo = stockInfoMapper.selectCcStockInfoById(info.getId());
        if(stockInfo.getLockNum().add(info.getCkNum()).compareTo(stockInfo.getKcNum())==1){
            throw new ServiceException(info.getItemCode()+":可用数量已超出库存数量！");
        }
        stockInfo.setLockNum(stockInfo.getLockNum().add(info.getCkNum()));
        stockInfo.setKyNum(stockInfo.getKyNum().subtract(info.getCkNum()));
        stockInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(stockInfo.getKcNum()));
        stockInfoMapper.updateCcStockInfo(stockInfo);
    }
    public void smCkSaveZx(CcStockInfo stockInfo,CcChukuInfo ccChukuInfo){
        BeanUtils.copyProperties(stockInfo,ccChukuInfo);
        ccChukuInfo.setContractCode(stockInfo.getContractCode());
        ccChukuInfo.setOrderDanhao(stockInfo.getSaleOrderCode());
        ccChukuInfo.setCreateTime(DateUtils.getNowDate());
        ccChukuInfo.setCreateBy(SecurityUtils.getUsername());
        ccChukuInfo.setFactoryName(SecurityUtils.getFactoryName());
        ccChukuInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        ccChukuInfo.setCkState(1L);
        ccChukuInfo.setStockinfoId(stockInfo.getId());
        stockInfo.setChukuId(ccChukuInfo.getDanhao());
        stockInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(stockInfo.getKcNum()));
        stockInfoMapper.updateCcStockInfo(stockInfo);
        ccChukuInfoMapper.insertCcChukuInfo(ccChukuInfo);
    }
    public void smCkSave(CcStockInfo stockInfo,CcChukuInfo ccChukuInfo){
        BeanUtils.copyProperties(stockInfo,ccChukuInfo);
        ccChukuInfo.setContractCode(stockInfo.getContractCode());
        ccChukuInfo.setOrderDanhao(stockInfo.getSaleOrderCode());
        ccChukuInfo.setCreateTime(DateUtils.getNowDate());
        ccChukuInfo.setCreateBy(SecurityUtils.getUsername());
        ccChukuInfo.setFactoryName(SecurityUtils.getFactoryName());
        ccChukuInfo.setFactoryCode(SecurityUtils.getFactoryCode());
        ccChukuInfo.setCkState(1L);
        ccChukuInfo.setStockinfoId(stockInfo.getId());
        stockInfo.setChukuId(ccChukuInfo.getDanhao());
        stockInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(stockInfo.getKcNum()));
        stockInfoMapper.updateCcStockInfo(stockInfo);
        ccChukuInfoMapper.insertCcChukuInfo(ccChukuInfo);
    }
    /*添加出库的库存信息*/
    @Override
    @Transactional
    public List<CcChukuInfo> addChuKuStockinfo(Long[] ids) {
        List<CcChukuInfo> ccChukuInfoList = new ArrayList<>();
        List<CcStockInfo> ccStockInfoList = stockInfoMapper.selectCcStockinfoInfoByIds(ids);
        if (ccStockInfoList.size() > 0) {
            for (CcStockInfo info : ccStockInfoList) {
                CcChukuInfo ccChukuInfo = new CcChukuInfo();
                ccChukuInfo.setId(info.getId());
                ccChukuInfo.setItemCode(info.getItemCode());
                ccChukuInfo.setItemId(info.getItemId());
                ccChukuInfo.setItemName(info.getItemName());
                ccChukuInfo.setItemNameEn(info.getItemNameEn());
                ccChukuInfo.setClassId(info.getClassId());
                ccChukuInfo.setClassName(info.getClassName());
                ccChukuInfo.setSpec(info.getSpec());
                ccChukuInfo.setSupplyId(info.getSupplyId());
                ccChukuInfo.setSupplyName(info.getSupplyName());
                ccChukuInfo.setUnit(info.getUnit());
                ccChukuInfo.setCkNum(info.getKcNum().subtract(info.getLockNum()));
                ccChukuInfo.setKyNum(info.getKcNum().subtract(info.getLockNum()));
                ccChukuInfo.setStockId(info.getStockId());
                ccChukuInfo.setKuquCode(info.getKuquCode());
                ccChukuInfo.setKuquName(info.getKuquName());
                ccChukuInfo.setKuweiCode(info.getKuweiCode());
                ccChukuInfo.setStockName(info.getStockName());
                ccChukuInfo.setUnitPrice(info.getUnitPrice());
                ccChukuInfo.setLot(info.getLot());
                ccChukuInfo.setQualityState(info.getQualityState());
                ccChukuInfo.setTxm(info.getTxm());
                ccChukuInfo.setUseState(info.getUseState());
                ccChukuInfo.setOrderDanhao(info.getSaleOrderCode());
                ccChukuInfo.setContractCode(info.getContractCode());
                ccChukuInfo.setPlanId(info.getRukuId());
                ccChukuInfoList.add(ccChukuInfo);
            }
        }
        return ccChukuInfoList;
    }

    @Override
    public List<CcChukuInfo> copyChukudetail(Long[] ids) {
        List<CcChukuInfo> ccChukuInfoList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            CcChukuInfo info = ccChukuInfoMapper.selectCcChukuInfoById(ids[i]);
            ccChukuInfoList.add(info);
//            CcChukuInfo info = ccChukuInfoMapper.selectCcChukuInfoById(ids[i]);
//            CcStockInfo queryStockInfo=new CcStockInfo();
//            queryStockInfo.setItemId(info.getItemId());
//            queryStockInfo.setKuweiCode("PJ00001");
//            //先进先出批次正序
//            List<CcStockInfo> stockInfoList = stockInfoMapper.selectCcStockInfoList2(queryStockInfo);
//            if(stockInfoList.size()>0){
//                //待出库数量
//                BigDecimal dckNum=info.getCkNum();
//                for (CcStockInfo stockInfo:stockInfoList){
//                    //1.先计算可用数量
//                    BigDecimal  kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
//                    //2.如果库存够直接扣除
//                    if(kyNum.compareTo(dckNum)>-1){
//                        //待执行接口
//                        info.setKyNum(stockInfo.getKyNum().subtract(dckNum));
//                        info.setCkNum(dckNum);
//                        info.setStockinfoId(stockInfo.getId());
//                        ccChukuInfoList.add(info);
//                        dckNum=new BigDecimal("0");
//                        break; // 退出循环
//                    }else{
//                        //否则不够库存继续循环
//                        //待执行接口
//                        info.setKyNum(stockInfo.getKyNum().subtract(kyNum));
//                        info.setCkNum(kyNum);
//                        info.setStockinfoId(stockInfo.getId());
//                        //不执行，直接出库
//                        ccChukuInfoList.add(info);
//                        dckNum= dckNum.subtract(kyNum);
//                    }
//                }
//                if(dckNum.compareTo(new BigDecimal("0"))>0){
//                    info.setKyNum(new BigDecimal("0"));
//                    info.setCkNum(dckNum);
//                    ccChukuInfoList.add(info);
//                }
//            }else {
//                info.setKyNum(new BigDecimal("0"));
//                ccChukuInfoList.add(info);
//            }
        }
        return  ccChukuInfoList;
    }

    @Override
    public List<CcChukuInfo> chukuPz(CcChukuInfo ccChukuInfo) {
        // 根据传过来的字符串danhao 调用selectCcChukuInfoByDanhao得到list
        CcChukuInfo param = new CcChukuInfo();
        param.setDanhao(ccChukuInfo.getDanhao());
        List<CcChukuInfo> ccChukuInfoList = ccChukuInfoMapper.selectCcChukuInfoList(param);

        // 在循环中给list的setPzState赋值1
        for (CcChukuInfo info : ccChukuInfoList) {
            info.setPzState(ccChukuInfo.getPzState());
            ccChukuInfoMapper.updateCcChukuInfo(info);
        }

        return ccChukuInfoList;
    }

    /**
     * 修改出库单
     *
     * @param ccChukuInfo 出库单
     * @return 结果
     */
    @Override
    public int updateCcChukuInfo(CcChukuInfo ccChukuInfo)
    {
        ccChukuInfo.setUpdateTime(DateUtils.getNowDate());
        return ccChukuInfoMapper.updateCcChukuInfo(ccChukuInfo);
    }

    /**
     * 批量删除出库单
     *
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCcChukuInfoByIds(Long[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            CcChukuInfo info = ccChukuInfoMapper.selectCcChukuInfoById(ids[i]);
            //初始化未执行撤销
            if(info.getCkState().longValue()==0L){
            CcStockInfo stockInfo = stockInfoMapper.selectCcStockInfoById(info.getStockinfoId());
            stockInfo.setLockNum(stockInfo.getLockNum().subtract(info.getCkNum()));
            stockInfo.setKyNum(stockInfo.getKyNum().add(info.getCkNum()));
            stockInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(stockInfo.getKcNum()));
                stockInfoMapper.updateCcStockInfo(stockInfo);
            ccChukuInfoMapper.deleteCcChukuInfoById(ids[i]);
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int zhixingChuku(Long[] ids) {
        for (int i = 0; i <ids.length ; i++) {
            CcChukuInfo info = ccChukuInfoMapper.selectCcChukuInfoById(ids[i]);
            //初始化未执行的
            if(info.getCkState().longValue()==0L){
                //查询库存的物料
                CcStockInfo stockInfo = stockInfoMapper.selectCcStockInfoById(info.getStockinfoId());
                //减掉库存数量
                if(stockInfo.getKcNum().compareTo(info.getCkNum())==1 || stockInfo.getKcNum().compareTo(info.getCkNum())==0){
                    stockInfo.setUpdateBy(SecurityUtils.getUsername());
                    stockInfo.setUpdateTime(new Date());
                    stockInfo.setKcNum(stockInfo.getKcNum().subtract(info.getCkNum()));
                    stockInfo.setLockNum(stockInfo.getLockNum().subtract(info.getCkNum()));
                    stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                    stockInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(stockInfo.getKcNum()));
                    stockInfo.setChukuId(info.getDanhao());
                    stockInfoMapper.updateCcStockInfo(stockInfo);
                }else {
                    throw new ServiceException("库存数量及出库数量错误！");
                }
                info.setCkState(1L);
                ccChukuInfoMapper.updateCcChukuInfo(info);
            }
        }
        return 1;
    }

    /**
     * 删除出库单信息
     *
     * @param id 出库单主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuInfoById(Long id)
    {
        return ccChukuInfoMapper.deleteCcChukuInfoById(id);
    }

    @Override
    public int handleAccount(Long[] ids) {
        for(long id :ids){
            CcChukuInfo ccChukuInfo = ccChukuInfoMapper.selectCcChukuInfoById(id);
            NcSalesOrder ncSalesOrder = new NcSalesOrder();
            ncSalesOrder.setOrderNumber(ccChukuInfo.getOrderDanhao());
            ncSalesOrder.setItemCode(ccChukuInfo.getItemCode());
//            ncSalesOrder.setContractUnitPrice(ccChukuInfo.getUnitPrice());
            List<NcSalesOrder> ncSalesOrders = ncSalesOrderMapper.selectNcSalesOrderList(ncSalesOrder);
            if(ncSalesOrders.size()>0){
                NcSalesOrder ncSalesOrderVo = ncSalesOrders.get(0);
                ZcOrderCostAccounting zcOrderCostAccounting = new ZcOrderCostAccounting();
                zcOrderCostAccounting.setCompanyCode(ncSalesOrderVo.getFactoryCode());
                zcOrderCostAccounting.setCompanyName(ncSalesOrderVo.getFactoryName());
                zcOrderCostAccounting.setOrderType(ncSalesOrderVo.getOrderType());
                zcOrderCostAccounting.setOrderNumber(ncSalesOrderVo.getOrderNumber());
                zcOrderCostAccounting.setProjectUnit(ncSalesOrderVo.getClientName());
                zcOrderCostAccounting.setMaterialType(ncSalesOrderVo.getProductType());
                zcOrderCostAccounting.setMaterialNumber(ncSalesOrderVo.getItemCode());
                zcOrderCostAccounting.setMaterialName(ncSalesOrderVo.getItemName());
                zcOrderCostAccounting.setSpecification(ncSalesOrderVo.getProductModel());
                zcOrderCostAccounting.setMaterialUnit(ncSalesOrderVo.getUnit());
                zcOrderCostAccounting.setBarcode(ncSalesOrderVo.getItemCode());
                zcOrderCostAccounting.setOrderQuantity(new BigDecimal(ncSalesOrderVo.getNumber()));
                zcOrderCostAccounting.setOutboundQuantity(ccChukuInfo.getCkNum());
                zcOrderCostAccounting.setOutboundNumber(ccChukuInfo.getDanhao());
                zcOrderCostAccounting.setOutboundDate(ccChukuInfo.getChukuDate());
                zcOrderCostAccounting.setSettlementNumber(ccChukuInfo.getId()+"");
                zcOrderCostAccounting.setSettlementQuantity(ccChukuInfo.getCkNum());
                zcOrderCostAccounting.setSettlementDate(ccChukuInfo.getChukuDate());
                zcOrderCostAccounting.setUnitPriceExclTax(ccChukuInfo.getUnitPrice());
                zcOrderCostAccounting.setAmountExclTax(ccChukuInfo.getTotalPrice());
                zcOrderCostAccounting.setUnitPriceInclTax(ccChukuInfo.getUnitPrice());
                zcOrderCostAccounting.setAmountInclTax(ccChukuInfo.getTotalPrice());
                zcOrderCostAccounting.setPlannedUnitPriceExclTax(ncSalesOrderVo.getContractUnitPrice());
                zcOrderCostAccounting.setPlannedAmountExclTax(ncSalesOrderVo.getContractUnitPrice().multiply(new BigDecimal(ncSalesOrderVo.getNumber())));
                zcOrderCostAccounting.setDifference(zcOrderCostAccounting.getPlannedAmountExclTax().subtract(zcOrderCostAccounting.getAmountExclTax()));
                zcOrderCostAccounting.setAccountingDate(new Date());
                zcOrderCostAccounting.setAccountant(SecurityUtils.getUsername());
                zcOrderCostAccounting.setCompletionProjectRate(zcOrderCostAccounting.getOutboundQuantity().divide(zcOrderCostAccounting.getOrderQuantity(),BigDecimal.ROUND_HALF_UP));
                zcOrderCostAccounting.setCompletionInvestmentRate(zcOrderCostAccounting.getAmountExclTax().divide(zcOrderCostAccounting.getPlannedAmountExclTax(),BigDecimal.ROUND_HALF_UP));
                zcOrderCostAccounting.setAccountingStatus("明细");
                zcOrderCostAccounting.setCreateTime(new Date());
                zcOrderCostAccounting.setCreateBy(SecurityUtils.getUsername());
                zcOrderCostAccounting.setDeptId(SecurityUtils.getDeptId());
                zcOrderCostAccountingMapper.insertZcOrderCostAccounting(zcOrderCostAccounting);
            }
        }
        return 1;
    }

    @Override
    public int handleSummary(Long[] ids) {
        for(long id :ids){
            CcChukuInfo ccChukuInfo = ccChukuInfoMapper.selectCcChukuInfoById(id);
            NcSalesOrder ncSalesOrder = new NcSalesOrder();
            ncSalesOrder.setOrderNumber(ccChukuInfo.getOrderDanhao());
            ncSalesOrder.setItemCode(ccChukuInfo.getItemCode());
//            ncSalesOrder.setContractUnitPrice(ccChukuInfo.getUnitPrice());
            List<NcSalesOrder> ncSalesOrders = ncSalesOrderMapper.selectNcSalesOrderList(ncSalesOrder);
            if(ncSalesOrders.size()>0){
                NcSalesOrder ncSalesOrderVo = ncSalesOrders.get(0);
                ZcOrderCostSummary zcOrderCostSummary = new ZcOrderCostSummary();
                zcOrderCostSummary.setCompanyCode(ncSalesOrderVo.getFactoryCode());
                zcOrderCostSummary.setCompanyName(ncSalesOrderVo.getFactoryName());
                zcOrderCostSummary.setOrderType(ncSalesOrderVo.getOrderType());
                zcOrderCostSummary.setOrderNumber(ncSalesOrderVo.getOrderNumber());
                zcOrderCostSummary.setProjectUnit(ncSalesOrderVo.getClientName());
                zcOrderCostSummary.setMaterialType(ncSalesOrderVo.getProductType());
                zcOrderCostSummary.setMaterialNumber(ncSalesOrderVo.getItemCode());
                zcOrderCostSummary.setMaterialName(ncSalesOrderVo.getItemName());
                zcOrderCostSummary.setSpecification(ncSalesOrderVo.getProductModel());
                zcOrderCostSummary.setMaterialUnit(ncSalesOrderVo.getUnit());
                zcOrderCostSummary.setBarcode(ncSalesOrderVo.getItemCode());
                zcOrderCostSummary.setOrderQuantity(new BigDecimal(ncSalesOrderVo.getNumber()));
                zcOrderCostSummary.setOutboundQuantity(ccChukuInfo.getCkNum());
                zcOrderCostSummary.setOutboundNumber(ccChukuInfo.getDanhao());
                zcOrderCostSummary.setOutboundDate(ccChukuInfo.getChukuDate());
                zcOrderCostSummary.setSettlementNumber(ccChukuInfo.getId()+"");
                zcOrderCostSummary.setSettlementQuantity(ccChukuInfo.getCkNum());
                zcOrderCostSummary.setSettlementDate(ccChukuInfo.getChukuDate());
                zcOrderCostSummary.setUnitPriceExclTax(ccChukuInfo.getUnitPrice());
                zcOrderCostSummary.setAmountExclTax(ccChukuInfo.getTotalPrice());
                zcOrderCostSummary.setUnitPriceInclTax(ccChukuInfo.getUnitPrice());
                zcOrderCostSummary.setAmountInclTax(ccChukuInfo.getTotalPrice());
                zcOrderCostSummary.setPlannedUnitPriceExclTax(ncSalesOrderVo.getContractUnitPrice());
                zcOrderCostSummary.setPlannedAmountExclTax(ncSalesOrderVo.getContractUnitPrice().multiply(new BigDecimal(ncSalesOrderVo.getNumber())));
                zcOrderCostSummary.setDifference(zcOrderCostSummary.getPlannedAmountExclTax().subtract(zcOrderCostSummary.getAmountExclTax()));
                zcOrderCostSummary.setAccountingDate(new Date());
                zcOrderCostSummary.setAccountant(SecurityUtils.getUsername());
                zcOrderCostSummary.setCompletionProjectRate(zcOrderCostSummary.getOutboundQuantity().divide(zcOrderCostSummary.getOrderQuantity(),BigDecimal.ROUND_HALF_UP));
                zcOrderCostSummary.setCompletionInvestmentRate(zcOrderCostSummary.getAmountExclTax().divide(zcOrderCostSummary.getPlannedAmountExclTax(),BigDecimal.ROUND_HALF_UP));
                zcOrderCostSummary.setCreateTime(new Date());
                zcOrderCostSummary.setCreateBy(SecurityUtils.getUsername());
                zcOrderCostSummary.setDeptId(SecurityUtils.getDeptId());
                zcOrderCostSummaryMapper.insertZcOrderCostSummary(zcOrderCostSummary);
            }
        }
        return 1;
    }
}
